<?php

namespace App\Http\Controllers;

use App\Http\Logic\AccessTokenLogic;
use App\Http\Logic\UserLogic;
use App\Http\Requests\UserRequest;
use App\Models\SwtOrder;
use Illuminate\Support\Facades\Storage;

class UserController extends BaseController
{
    /**
     * @notes 获取用户信息
     * @param UserRequest $request
     * @return \Dingo\Api\Http\Response
     * @author Evil丿
     * @date 2024/06/14 15:32
     */
    public function getUserInfo(UserRequest $request)
    {
        $user_info = $request -> user();

        if(!$user_info -> status) {
            return $this->error('用户已被禁用', 257);
        }

        if(!$user_info -> avatar) {
            $user_info -> avatar = 'images/logo.png';
        }

        if(!$user_info -> name) {
            $user_info -> name = '微信用户-'.$user_info->id;
        }

        $is_card_user = UserLogic::getCardUserInfo($user_info -> id);

        $data = [
            'uid'               => $user_info->id,
            'avatar'            => config('app.url') . Storage::url($user_info->avatar),
            'name'              => $user_info->name,
            'mobile'            => $user_info->mobile,
            'further_pay_money' => 0,
            'is_card_user'      => $is_card_user ? 1 : 0,
            'need_pay_order_id' => ""
        ];

        $hasPayOrder = SwtOrder::where('user_id', $user_info -> id) -> where('need_further_pay', 1) -> first();

        if ($hasPayOrder) {
            $data['further_pay_money'] = $hasPayOrder -> further_pay_money;
            $data['need_pay_order_id'] = $hasPayOrder -> id;
        }

        return $this->success($data);
    }

    /**
     * @notes 上传用户头像
     * @param UserRequest $request
     * @return \Dingo\Api\Http\Response
     * @author Evil丿
     * @date 2024/06/14 15:34
     */
    public function uploadAvatar(UserRequest $request)
    {
        $file = $request -> file('avatar');

        $user_info = $request -> user();

        $file_path = 'public/upload/avatar';

        $path = $file -> store($file_path);

        if(!$path) {
            return $this->error('文件上传失败');
        }

        UserLogic::editUserInfo($user_info -> id, ['avatar' => $path]);

        return $this->success(['uri' => config('app.url'). '/' . $path ]);
    }

    /**
     * @notes 设置用户昵称
     * @param UserRequest $request
     * @return \Dingo\Api\Http\Response
     * @author Evil丿
     * @date 2024/06/14 15:35
     */
    public function setUserName(UserRequest $request)
    {
        $user_info = $request -> user();

        $name = $request->input('name');

        UserLogic::editUserInfo($user_info -> id, ['name' => $name]);

        return $this->success();
    }

    /**
     * @notes 退出登录
     * @param UserRequest $request
     * @return \Dingo\Api\Http\Response
     * @author Evil丿
     * @date 2024/06/14 15:36
     */
    public function logout(UserRequest $request) {
        $user_info = $request -> user();

        AccessTokenLogic::delAccessToken($user_info);

        return $this->success();
    }
}
